<template>
  <div>
    <h2>编辑工作</h2>
    <el-form :model="jobForm" :rules="rules" ref="jobForm" label-width="100px">
      <el-form-item label="ID" prop="id">
        <el-input v-model="jobForm.id" disabled></el-input>
      </el-form-item>
      <el-form-item label="Name" prop="name">
        <el-input v-model="jobForm.name"></el-input>
      </el-form-item>
      <el-form-item label="Status" prop="status">
        <el-select v-model="jobForm.status">
          <el-option
              v-for="status in statusOptions"
              :key="status.value"
              :label="status.label"
              :value="status.value"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="Description" prop="description">
        <el-input v-model="jobForm.description"></el-input>
      </el-form-item>
      <el-form-item label="创建时间" prop="create_time">
        <el-date-picker
            v-model="jobForm.create_time"
            type="datetime"
            placeholder="选择日期时间"
            format="yyyy-MM-dd HH:mm:ss"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="结束时间" prop="end_time">
        <el-date-picker
            v-model="jobForm.end_time"
            type="datetime"
            placeholder="选择日期时间"
            format="yyyy-MM-dd HH:mm:ss"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSubmit">保存</el-button>
        <el-button @click="onReset">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import axios from 'axios';
export default {
  props: {
    id: {
      type: Number,
      required: true,
    },
    job: {
      type: String,
      required: false,
    },
  },
  data() {
    return {
      jobForm: {},
      rules: {
        name: [{ required: true, message: '请输入工作名称', trigger: 'blur' }],
        status: [{ required: true, message: '请选择工作状态', trigger: 'change' }],
        description: [{ required: true, message: '请输入工作描述', trigger: 'blur' }],
        create_time: [{ required: true, message: '请选择创建时间', trigger: 'change' }],
        end_time: [{ required: true, message: '请选择结束时间', trigger: 'change' }],
      },
      statusOptions: [
        { value: 'active', label: '活跃' },
        { value: 'inactive', label: '不活跃' },
        { value: 'completed', label: '已完成' },
      ],
    };
  },
  /*  async mounted() {
      if (this.job) {
        this.jobForm = JSON.parse(this.job);
      } else {
        // 如果job不存在，可能是新建操作，从服务器获取初始数据
        try {
          const response = await axios.get(`/api/jobs/${this.id}`);
          if (response.data) {
            this.jobForm = response.data;
          }
        } catch (error) {
          console.error('Error fetching initial job data:', error);
        }
      }
    },*/
  methods: {
    onSubmit() {
      this.$refs.jobForm.validate(async (valid) => {
        if (valid) {
          try {
            const response = await axios.post(`/api/update_job`, this.jobForm);
            if (response.data.code === 200) {
              this.$message.success('工作信息已成功更新');
              this.$router.push('/job-list');
            } else {
              this.$message.error('更新失败，请稍后重试');
            }
          } catch (error) {
            this.$message.error('服务器错误，请稍后重试');
            console.error('Error updating job:', error);
          }
        } else {
          this.$message.error('请检查并修正表单');
        }
      });
    },
    onReset() {
      this.$refs.jobForm.resetFields();
      this.$message.info('表单已重置');
    },
  },
};
</script>
